const fund = [
  { code: "001606", name: "农银工业4.0混合" },
  { code: "519674", name: "银河创新成长混合A" },
  { code: "004241", name: "中欧时代先锋股票C" },
  { code: "003096", name: "中欧医疗健康混合C" },
  { code: "270002", name: "广发稳健增长混合A" },
  { code: "162605", name: "景顺长城鼎益混合(LOF)" },
];

const axios = require("axios");

/** 历史净值 */
async function valueOfHistory(fundCode, days) {
  const res = await axios(
    `http://api.fund.eastmoney.com/f10/lsjz?fundCode=${fundCode}&pageIndex=1&pageSize=${days}`,
    { headers: { Referer: `http://fundf10.eastmoney.com` } }
  );

  return res.data.Data.LSJZList;
}

/** 净值估算 */
async function estimate(fundCode) {
  const res = await axios(`http://fundgz.1234567.com.cn/js/${fundCode}.js`);
  return JSON.parse(res.data.slice(8, -2));
}

fund.forEach((f) => main(f.code));
async function main(fundCode) {
  const res = [
    { days: 30, than: 0 },
    { days: 90, than: 0 },
    { days: 180, than: 0 },
    { days: 360, than: 0 },
    // { days: 960, than: 0 },
  ];

  const list = await valueOfHistory(fundCode, res[res.length - 1].days);
  const gz = await estimate(fundCode);

  list.forEach((e, i) => {
    // 高于净值
    if (gz.gsz > e.DWJZ) {
      res.forEach((r) => i < r.days && r.than++);
    }
  });

  console.log(
    `
${gz.fundcode} ${gz.name}
当前估值 ${gz.gsz} ${(gz.gszzl > 0 ? "+" : "") + gz.gszzl}% (${gz.gztime})
高于之前${res.map((r) => ` ${r.than}/${r.days} 天`)}`
  );
}
